// components/cm-upload/index.js
import {
  config
} from "../../utils/config.js";
const nxItem = require('../../behaviors/form/nx-item');
const app = getApp();
Component({
  behaviors: [nxItem],
  /**
   * 组件的属性列表
   */
  properties: {
    fileList: {
      type: Array,
      value: true
    },
    fileQty: {
      type: Number,
      value: 0,
    },
    templateUrl: {
      type: String,
      value: '',
    },
    type: {
      type: Number,
      value: null,
    },
    disabled: {
      type: Boolean,
      value: false
    },
    isLabelType: {
      type: Boolean,
      value: false
    },

  },

  /**
   * 组件的初始数据
   */
  data: {},

  /**
   * 组件的方法列表
   */
  methods: {
    beforeRead(event) {
      const type = this.data.type
      const {
        file,
        callback
      } = event.detail;
      const modelType = type == 10 ? 'file' : 'image'
      if (file.type != modelType) {
        wx.showToast({
          title: `只支持上传${type == 10 ? '文件' : '图片'}`,
          icon: "none",
        });
        return;
      }
      if (file.size > 5 * 1024 * 1024) {
        wx.showToast({
          title: "图片大小不能超过5M",
          icon: "none",
        });
        return;
      }
      callback(true);
    },
    async afterRead(event) {
      const {
        file
      } = event.detail;
      const data = await this.upload(file);
      if (data.success) {
        wx.showToast({
          title: "上传成功！",
          icon: "success",
        });
        const {
          fileList
        } = this.data
        fileList.push({
          ...file,
          url: data.data.src,
          AttachmentId: data.data.id
        });
        this.setData({
          fileList,
        });
        //因为需要单个输入变化，且数据接口不统一，所以手动赋值errorMessage
        const combinData={
          errorMessage:'',
          fileList
        }
        this.triggerEvent('receiveFile', combinData)
      } else {
        wx.showToast({
          title: "上传失败，请重新上传",
          icon: "none",
        });
      }
    },
    upload(file) {
      const headHttp = config.baseUrl
      return new Promise((resolve, reject) => {
        wx.uploadFile({
          url: headHttp + "/UserResource/HuaWeiObsTempUploadImage",
          filePath: file.url,
          name: "file",
          formData: {
            user: "test"
          },
          header: {
            Authorization: "Bearer " + app.globalData.token,
            "content-type": "application/x-www-form-urlencoded",
          },
          success(res) {
            if (res.data) {
              resolve(JSON.parse(res.data));
            }
          },
          fail(err) {
            console.log(err);
            wx.showToast({
              title: "请求失败,请刷新后重试",
              icon: "none"
            });
            reject(err);
          },
        });
      });
    },
    deletImg(e) {
      const {
        fileList
      } = this.data
      fileList.splice(e.detail.index, 1)
      this.setData({
        fileList
      })
       //因为需要单个输入变化，且数据接口不统一，所以手动赋值errorMessage
      const combinData={
        errorMessage:'不能为空！',
        fileList
      }
      this.triggerEvent('receiveFile', combinData)
    },
  }
})